package com.wzs.workbench.service.impl;

import com.wzs.utils.UUIDUtil;
import com.wzs.vo.PaginationVO;
import com.wzs.workbench.dao.ChapterMapper;
import com.wzs.workbench.dao.NovelMapper;
import com.wzs.workbench.domain.Chapter;
import com.wzs.workbench.domain.Novel;
import com.wzs.workbench.service.NovelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class NovelServiceImpl implements NovelService {

    @Resource
    private NovelMapper novelMapper;

    @Resource
    private ChapterMapper chapterMapper;


    @Override
    public PaginationVO<Chapter> pageList(HashMap<String, Object> map) {
        //取得total
        int total = chapterMapper.getTotalByCondition(map);
        //取得list
        List<Chapter> dataList = chapterMapper.getChapterListByCondition(map);
        //创建一个vo对象,将total和datalist封装到vo中
        PaginationVO<Chapter> vo = new PaginationVO<>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    @Override
    public PaginationVO<Novel> novelPageList(HashMap<String, Object> map) {
        //取得total
        int total = novelMapper.getTotalByCondition(map);
        //取得list
        List<Novel> dataList = novelMapper.getNovelListByCondition(map);
        //创建一个vo对象,将total和datalist封装到vo中
        PaginationVO<Novel> vo = new PaginationVO<>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    @Override
    public PaginationVO<Novel> pageListByAid(HashMap<String, Object> map) {
        //取得total
        int total = novelMapper.getTotalByAid(map);
        //取得list
        List<Novel> dataList = novelMapper.getNovelListByAid(map);
        //创建一个vo对象,将total和datalist封装到vo中
        PaginationVO<Novel> vo = new PaginationVO<>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    @Override
    public boolean putNovel(Novel novel) {

        boolean flag = true;

        String id = UUIDUtil.getUUID();
        novel.setId(id);

        int count = novelMapper.insert(novel);

        if (count != 1){
            flag = false;
        }

        return flag;
    }

    @Override
    public boolean deleteNovel(String[] ids) {
        boolean flag = true;

        //删除读者信息
        int count3 = novelMapper.deleteNovel(ids);
        if (count3 != ids.length) {
            flag = false;
        }
        return flag;
    }

    @Override
    public Novel getNovelList(String id) {
        //取List<Novel>
        return novelMapper.getNovelList(id);
    }

    @Override
    public boolean updateNovel(Novel novel) {

        boolean flag = true;

        int count = novelMapper.updateNovel(novel);

        if (count != 1){
            flag = false;
        }

        return flag;
    }

    @Override
    public Novel detail(String id) {
        return novelMapper.detail(id);
    }

    @Override
    public List<String> getNovelName(String name) {
        return novelMapper.getNovelName(name);
    }

    @Override
    public Novel getById(String novelId) {
        return novelMapper.selectByPrimaryKey(novelId);
    }

}
